package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Orders;
import com.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @RequestMapping("/findAll")
    public String find(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page,  @RequestParam(value = "limit",defaultValue = "5")int limit,@RequestParam(value = "key",required = false) String key,@RequestParam(value = "value",required = false)String value){
        String ordersNum="";
        String productName="";
        if ("orderNum".equals(key)){
            ordersNum=value;
        }else if("productName".equals(key)){
            productName=value;
        }
        PageHelper.startPage(page,limit);
        List<Orders> all = ordersService.findAllOrders(ordersNum,productName);
        PageInfo<Orders> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("select",key);
        request.setAttribute("selectValue",value);
        return "/orders/orders-list";
    }

    @RequestMapping("/findById")
    public String findById(HttpServletRequest request,int id){
        Orders byId = ordersService.findById(id);
        request.setAttribute("order",byId);
        return "/orders/orders-show";
    }
}
